package com.mcxx.modules.miniSalvation.fundpay.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.FundPayConvert;
import com.mcxx.modules.miniSalvation.fundpay.dao.write.FundPayWriteDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.*;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@TaTransactional
public class FundPayWriteService {

    @Autowired
    private FundPayWriteDao fundPayWriteDao;
    @Autowired
    private FundPayFamilyWriteService fundPayFamilyWriteService;
    @Autowired
    private FundPayMemberWriteService fundPayMemberWriteService;



    public int updateIgnoreNull(FundPayEntity fundPayEntity){
        return fundPayWriteDao.updateIgnoreNull(fundPayEntity);
    }


    public int updateStateById(String fundPayId, String state) {
        return fundPayWriteDao.updateStateById(fundPayId, state);
    }


    public void confirmFundPay(String fundPayId, UserAccountVo userAccountVo){
        FundPayEntity fundPayEntity = new FundPayEntity();
        fundPayEntity.setId(fundPayId);
        fundPayEntity.setStatus(Constant.FundPayState.CONFIRM);
        fundPayEntity.setCommitDate(new Date());
        fundPayEntity.setCheckPerson(userAccountVo.getUserId());
        int rst = this.updateIgnoreNull(fundPayEntity);
        if(rst <= 0){
            throw new BizException("-1", "名册确认失败");
        }
    }


    public FundPayEntity insertCapital(CapitalFundPayDTO capitalFundPayDTO, UserAccountVo userAccountVo) {

        capitalFundPayDTO.setId(Constant.getUUID());
        capitalFundPayDTO.setOrgId(userAccountVo.getOrgId());
        capitalFundPayDTO.setAreaCode(userAccountVo.getAreaCode());

        FundPayEntity fundPayEntity = FundPayConvert.convertFundPayEntity(capitalFundPayDTO);

        fundPayEntity.setStatus(Constant.FundPayState.CREATE_ING);

        if(null == capitalFundPayDTO.getIsAuto()) {
            fundPayEntity.setIsAuto(Constant.IsAuto.NO);
        }
        else {
            fundPayEntity.setIsAuto(capitalFundPayDTO.getIsAuto());
        }

        fundPayEntity.setOperator(userAccountVo.getUserId());

        this.insert(fundPayEntity);

        return fundPayEntity;
    }


    public FundPayEntity insertStand(StandFundPayDTO standFundPayDTO, UserAccountVo userAccountVo) {

        standFundPayDTO.setId(Constant.getUUID());
        standFundPayDTO.setOrgId(userAccountVo.getOrgId());
        standFundPayDTO.setAreaCode(userAccountVo.getAreaCode());

        FundPayEntity fundPayEntity = FundPayConvert.convertFundPayEntity(standFundPayDTO);

        fundPayEntity.setStatus(Constant.FundPayState.CREATE_ING);

        fundPayEntity.setIsAuto(Constant.IsAuto.NO);

        fundPayEntity.setOperator(userAccountVo.getUserId());

        this.insert(fundPayEntity);

        return fundPayEntity;
    }


    public void insertAllowance(AllowanceFundPayDTO allowanceFundPayDTO, UserAccountVo userAccountVo){

        allowanceFundPayDTO.setFundType(Constant.FundType.ALLOWANCE_FUND_PAY);

        allowanceFundPayDTO.setId(Constant.getUUID());

        FundPayEntity fundPayEntity = FundPayConvert.convertFundPayEntity(allowanceFundPayDTO);

        fundPayEntity.setStatus(Constant.FundPayState.CREATE_ING);

        fundPayEntity.setOrgId(userAccountVo.getOrgId());

        fundPayEntity.setAreaCode(userAccountVo.getAreaCode());

        fundPayEntity.setIsAuto(Constant.IsAuto.NO);

        fundPayEntity.setOperator(userAccountVo.getUserId());

        this.insert(fundPayEntity);
    }


    public void insertCustom(CustomFundPayDTO customFundPayDTO, UserAccountVo userAccountVo){

        customFundPayDTO.setId(Constant.getUUID());

        customFundPayDTO.setOrgId(userAccountVo.getOrgId());

        customFundPayDTO.setAreaCode(userAccountVo.getAreaCode());


        FundPayEntity fundPayEntity = FundPayConvert.convertFundPayEntity(customFundPayDTO);

        fundPayEntity.setStatus(Constant.FundPayState.CREATE);

        fundPayEntity.setIsAuto(Constant.IsAuto.NO);

        fundPayEntity.setOperator(userAccountVo.getUserId());

        this.insert(fundPayEntity);
    }


    public FundPayEntity insertFuneral(FuneralFundPayDTO funeralFundPayDTO, UserAccountVo userAccountVo) {

        funeralFundPayDTO.setId(Constant.getUUID());
        funeralFundPayDTO.setOrgId(userAccountVo.getOrgId());
        funeralFundPayDTO.setAreaCode(userAccountVo.getAreaCode());

        FundPayEntity fundPayEntity = FundPayConvert.convertFundPayEntity(funeralFundPayDTO);

        fundPayEntity.setStatus(Constant.FundPayState.CREATE_ING);

        fundPayEntity.setIsAuto(Constant.IsAuto.NO);

        fundPayEntity.setOperator(userAccountVo.getUserId());

        this.insert(fundPayEntity);

        return fundPayEntity;
    }


    public int insert(FundPayEntity fundPayEntity) {
        fundPayEntity.setCreateTime(new Date());
        int rst = fundPayWriteDao.insertIgnoreNull(fundPayEntity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_10043);
        }
        return rst;
    }


    public int delFundPay(List<String> fundPayIdList){

        fundPayMemberWriteService.deleteByFundPayIds(fundPayIdList);


        fundPayFamilyWriteService.deleteByFundPayIds(fundPayIdList);


        int rst = fundPayWriteDao.deleteByIds(fundPayIdList);
        if(rst <= 0){
            throw new BizException("-1", "名册删除异常");
        }

        return 1;
    }


    public int updateStateByIds(List<String> fundPayIdList, String state, Date commitDate){
        return fundPayWriteDao.updateStateByIds(fundPayIdList, state, commitDate);
    }

    public void updateFundPayNumAndStatus(String fundPayId, String status){
        int rst = fundPayWriteDao.updateFundPayNumAndStatus(fundPayId, status);
        if(rst <= 0){
            throw new BizException("名册信息更新失败");
        }
    }


    public void updateFundPayNum(String fundPayId){
        int rst = fundPayWriteDao.updateFundPayNumAndStatus(fundPayId, null);
        if(rst <= 0){
            throw new BizException("名册信息更新失败");
        }
    }



    public void updateFundNumber(String fundPayId){
        int rst = fundPayWriteDao.updateFundNumber(fundPayId);
        if(rst <= 0){
            throw new BizException("名册信息更新失败");
        }
    }


    public void updateFundFamilyNum(String fundPayId){
        int rst = fundPayWriteDao.updateFundFamilyNum(fundPayId);
        if(rst <= 0){
            throw new BizException("名册信息更新失败");
        }
    }

}
